<?php

declare(strict_types=1);

use think\migration\Migrator;

class CreateAuthTables extends Migrator
{
    public function change(): void
    {
        $this->createRoleTable();
        $this->createPermissionTable();
        $this->createRolePermissionPivot();
        $this->createUserRolePivot();
    }

    private function createRoleTable(): void
    {
        $this->table('auth_role', [
            'comment' => '角色定义表',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('name', 'string', [
                'limit' => 80,
                'comment' => '角色名称',
            ])
            ->addColumn('slug', 'string', [
                'limit' => 80,
                'comment' => '角色标识',
            ])
            ->addColumn('scope_type', 'string', [
                'limit' => 20,
                'default' => 'system',
                'comment' => '作用域 admin/seller/buyer/support/org',
            ])
            ->addColumn('org_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '组织ID（组织级角色使用）',
            ])
            ->addColumn('description', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '描述',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 0-禁用 1-启用',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['slug'], ['unique' => true])
            ->addIndex(['scope_type'])
            ->addIndex(['org_id'])
            ->create();
    }

    private function createPermissionTable(): void
    {
        $this->table('auth_permission', [
            'comment' => '权限定义表',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('name', 'string', [
                'limit' => 120,
                'comment' => '权限名称',
            ])
            ->addColumn('code', 'string', [
                'limit' => 150,
                'comment' => '权限编码',
            ])
            ->addColumn('description', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '描述',
            ])
            ->addColumn('client_type', 'string', [
                'limit' => 20,
                'default' => 'admin',
                'comment' => '客户端类型 admin/seller/support',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 0-禁用 1-启用',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['code'], ['unique' => true])
            ->addIndex(['client_type'])
            ->addIndex(['status'])
            ->create();
    }

    private function createRolePermissionPivot(): void
    {
        $this->table('auth_role_perm', [
            'comment' => '角色权限关联表',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('role_id', 'integer', [
                'signed' => false,
                'comment' => '角色ID',
            ])
            ->addColumn('permission_id', 'integer', [
                'signed' => false,
                'comment' => '权限ID',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['role_id'])
            ->addIndex(['permission_id'])
            ->addIndex(['role_id', 'permission_id'], ['unique' => true])
            ->create();
    }

    private function createUserRolePivot(): void
    {
        $this->table('auth_user_role', [
            'comment' => '用户角色关联表',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('user_id', 'integer', [
                'signed' => false,
                'comment' => '用户ID',
            ])
            ->addColumn('role_id', 'integer', [
                'signed' => false,
                'comment' => '角色ID',
            ])
            ->addColumn('org_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '组织ID（组织级角色使用）',
            ])
            ->addColumn('region_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '区域ID',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 0-禁用 1-启用',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['user_id'])
            ->addIndex(['role_id'])
            ->addIndex(['org_id'])
            ->addIndex(['region_id'])
            ->addIndex(['user_id', 'role_id', 'org_id'], ['unique' => true])
            ->create();
    }
}
